package labox.innovation.gameserver.model.actor;

import java.util.Collection;

import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.serverpackets.AbstractNpcInfo;
import labox.innovation.gameserver.network.serverpackets.L2GameServerPacket;
import labox.innovation.gameserver.templates.chars.CharTemplate;
import labox.innovation.gameserver.templates.chars.NpcTemplate;
import labox.innovation.gameserver.templates.item.L2Weapon;

/**
 * 陷阱类
 * @author zjj
 *
 */
public class L2Trap extends L2Character {
	
	private FuzePcInstance _owner;
	
	//陷阱是否被触发了
	protected boolean _isTriggered;

	/**
	 * @param objectId
	 * @param template
	 */
	public L2Trap(long objectId, CharTemplate template, FuzePcInstance owner) {
		super(objectId, template);
		setIsInvul(false);
		_owner = owner;
		setXYInvisible(owner.getX(), owner.getY());
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.L2Object#onAction(labox.innovation.gameserver.model.actor.instance.FuzePcInstance)
	 */
	@Override
	public void onAction(FuzePcInstance player) {
		player.setTarget(this);
	}
	
	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#onDecay()
	 */
	@Override
	public void onDecay() {
		deleteMe(_owner);
	}

	/**
	 * 
	 * @return
	 */
	public final int getNpcId() {
		return getTemplate()._id;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.L2Object#isAutoAttackable(labox.innovation.gameserver.model.actor.L2Character)
	 */
	@Override
	public boolean isAutoAttackable(L2Character attacker) {
		return _owner.isAutoAttackable(attacker);
	}

	/**
	 * 
	 * @param owner
	 */
	public void deleteMe(FuzePcInstance owner) {
		decayMe();
		getKnownList().removeAllKnownRegion();
		owner.setTrap(null);
	}

	/**
	 * 
	 * @param owner
	 */
	public synchronized void unSummon(FuzePcInstance owner) {
		if (isVisible() && !isDead()) {
			owner.setTrap(null);
			decayMe();
		}
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#getActiveWeaponInstance()
	 */
	@Override
	public L2ItemInstance getActiveWeaponInstance() {
		return null;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#getActiveWeaponItem()
	 */
	@Override
	public L2Weapon getActiveWeaponItem() {
		return null;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#getLevel()
	 */
	@Override
	public int getLevel() {
		return getTemplate().level;
	}

	/**
	 * 
	 * @return
	 */
	public final FuzePcInstance getOwner() {
		return _owner;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#getTemplate()
	 */
	@Override
	public NpcTemplate getTemplate() {
		return (NpcTemplate) super.getTemplate();
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#getSecondaryWeaponInstance()
	 */
	@Override
	public L2ItemInstance getSecondaryWeaponInstance() {
		return null;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#getSecondaryWeaponItem()
	 */
	@Override
	public L2Weapon getSecondaryWeaponItem() {
		return null;
	}

	@Override
	public FuzePcInstance getActingPlayer() {
		return _owner;
	}

	/**
	 * 
	 * @see labox.innovation.gameserver.model.actor.L2Character#updateAbnormalEffect()
	 */
	@Override
	public void updateAbnormalEffect() {

	}

	/**
	 * 角色是否能看到这个陷阱
	 * @param cha
	 * @return
	 */
	public boolean canSee(L2Character cha)
	{
		return false;
	}
	
	/**
	 * 向能看到 陷阱的角色 广播陷阱的存在
	 */
	@Override
	public void broadcastPacket(L2GameServerPacket mov)
	{
		Collection<FuzePcInstance> plrs = getKnownList().getKnownPlayers().values();
		for (FuzePcInstance player : plrs)
			if (player != null && (_isTriggered || canSee(player)))
				player.sendPacket(mov);
	}
	
	/**
	 * 
	 * @return
	 */
	public boolean isDetected() {
		return false;
	}

	/**
	 * 
	 *
	 */
	public void setDetected() {
		// Do nothing
	}
	
	/**
	 * 陷阱是否被触发
	 * @return
	 */
	public boolean isTriggered()
	{
		return _isTriggered;
	}
	
	public void triggered()
	{
		_isTriggered = true;
	}

	@Override
	public void sendInfo(FuzePcInstance activeChar) {
		if (_isTriggered || canSee(activeChar))
			//这个 最终 需要改成为 TrapInfo
			//TODO:
			activeChar.sendPacket(new AbstractNpcInfo.NpcInfo(null, activeChar));
	}
	
}

